package csc.training.jsf2.service;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import csc.training.jsf2.dao.MailSystemDao;
import csc.training.jsf2.dao.MailSystemDaoImpl;
import csc.training.jsf2.entity.Account;


/**
 * 
 * @author hdang20
 *
 */
@ManagedBean
@SessionScoped
public class LoginServiceImpl implements LoginService, Serializable{
	private static final long serialVersionUID = 6720998746684832303L;

	public boolean checkToLogin(String emailAddress, String password){		
		return checkEmailLogin(emailAddress) & checkPassword(password);
	}
	
	public boolean checkEmailLogin(String emailAddress){
		MailSystemDao mailSystem = new MailSystemDaoImpl();
		List<Account> accounts = mailSystem.getAccounts();
		for (Account account : accounts) {
			if(account.getEmailAddress().equals(emailAddress)){
				return true;
			}
		}
		return false;
	}
	
	public boolean checkPassword(String password){
		MailSystemDao mailSystem = new MailSystemDaoImpl();
		List<Account> accounts = mailSystem.getAccounts();
		for (Account account : accounts) {
			if(account.getPassword().equals(password)){
				return true;
			}
		}
		return false;
	}
	
	public Account getAccount(String emailAddress, String password){
		MailSystemDao mailSystem = new MailSystemDaoImpl();
		List<Account> accounts = mailSystem.getAccounts();
		for (Account account : accounts) {
			if( account.getEmailAddress().equals(emailAddress)
					&& account.getPassword().equals(password)){
				return account;
			}
		}
		return null;
	}

}
